/*
 * EasyBounceMainContainer.cpp
 *
 *  Created on: 2009-3-14
 *      Author: Wayne
 */

#include <eikbtgpc.h>
// to enable skin support for the application
#include <AknsDrawUtils.h>
#include <AknsBasicBackgroundControlContext.h>

#include <EasyBounce.rsg>

#include "EasyBounceMainContainer.h"
#include "EasyBounceAppUi.h"
#include "EasyBounceMainview.h"
#include "EasyBounce.hrh"

#include "coedef.h"

// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// ConstructL(const TRect& aRect)
// Two phased constructor
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();
    
    CreateLableL();
    CreateButtonsL();

   	iBgContext = CAknsBasicBackgroundControlContext::NewL( 
   			KAknsIIDQsnBgAreaMain, aRect, ETrue);
   	
    SetRect(aRect);
    ActivateL();
    }

// Destructor
CEasyBounceMainContainer::~CEasyBounceMainContainer()
    {
    delete iLabel;
    delete iStartButton;
    delete iRankButton;
    delete iBgContext;
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::SizeChanged()
    {

    iLabel->SetExtent( TPoint(10,10), TSize(0,0) );
    
    //Reset skin
    if(iBgContext)
		{
		iBgContext->SetRect(Rect());
        if ( &Window() )
			{
			iBgContext->SetParentPos( PositionRelativeToScreen() );
			}
		}
    
    //reset button size and position
    TRect containerRect = Rect();
    TPoint containerTL = containerRect.iTl;
    TInt h = containerRect.Height();
    TInt w = containerRect.Width();
    
    TSize buttonSize( w*1/3, h*2/15);
    
    TPoint startButtonTL ( containerTL.iX + w/3 , containerTL.iY + h/3 );
    TRect startButtonRect (startButtonTL, buttonSize);
    iStartButton->SetRect(startButtonRect);
    
    TPoint rankButtonTL ( startButtonTL.iX, startButtonTL.iY + h*3/15 );
    TRect rankButtonRect (rankButtonTL, buttonSize);
    iRankButton->SetRect(rankButtonRect);
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CEasyBounceMainContainer::CountComponentControls() const
    {
    return 3; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CEasyBounceMainContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
        	return iLabel;
        	break;
        case 1:
        	return iStartButton;
        	break;
        case 2:
        	return iRankButton;
        	break;
        default:
            return NULL;
        }
    }


// ---------------------------------------------------------
// CEasyBounceMainContainer::OffeKeyEventL()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
TKeyResponse CEasyBounceMainContainer::OfferKeyEventL(
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
    {
    TKeyResponse ret;

    ret = EKeyWasNotConsumed;

    return ret;
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbWhite);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    
    // Redraw the background using the default skin
     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
     AknsDrawUtils::Background( skin, cc, this, gc, aRect );
    
    gc.DrawRect(aRect);
    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::HandleControlEventL(
    CCoeControl* aControl, TCoeEvent aEventType)
    {
    switch ( aEventType ) 
        { 
            case EEventStateChanged:
            {
				//get referece of main view
				CEasyBounceAppUi* appUi = (CEasyBounceAppUi*)iEikonEnv->AppUi();
				TUid mainViewId = TUid::Uid(EMainViewId); 
				CEasyBounceMainView* view = (CEasyBounceMainView*)appUi->View(mainViewId);
			
                if(aControl == iStartButton)
    			{
    			//start a new game
    			view->CmdNewGameL();    			
    			}
    			else if(aControl == iRankButton)
    			{
                //switch to rank view
    			view->CmdShowRankL();
    			}
    		}
    		break; 
            default: 
                 break; 
    	}

    }

// ---------------------------------------------------------
// CEasyBounceMainContainer::MopSupplyObject()
// Needed for supporting skin     
// ---------------------------------------------------------
//
TTypeUid::Ptr CEasyBounceMainContainer::MopSupplyObject(TTypeUid aId)
{
	 if (iBgContext )
	 {
	   return MAknsControlContext::SupplyMopObject( aId, iBgContext );
	 }
	 return CCoeControl::MopSupplyObject(aId);
}

// ---------------------------------------------------------
// CEasyBounceMainContainer::HandleResourceChange()
// Handle resource change event   
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::HandleResourceChange(TInt aType)
	{
	//get referece of main view
	CEasyBounceAppUi* appUi = (CEasyBounceAppUi*)iEikonEnv->AppUi();
	TUid mainViewId = TUid::Uid(EMainViewId); 
	CEasyBounceMainView* view = (CEasyBounceMainView*)appUi->View(mainViewId);
	
	//let mainview to reset maincontainer's rect 
	view->HandleClientRectChange();
	}

// ---------------------------------------------------------
// CEasyBounceMainContainer::CreateLableL()
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::CreateLableL()
	{
    iLabel= new (ELeave) CEikLabel;
    iLabel->SetContainerWindowL( *this );
    iLabel->SetTextL( _L("MainView") );
    iLabel->OverrideColorL( EColorLabelTextEmphasis, KRgbYellow );
    iLabel->SetEmphasis( CEikLabel::EPartialEmphasis ); 	
	}

// ---------------------------------------------------------
// CEasyBounceMainContainer::CreateButtonsL()
// ---------------------------------------------------------
//
void CEasyBounceMainContainer::CreateButtonsL()
	{
	//create button from directly
	//set button rect at the same time
    _LIT(KStartButtonText,"Start");
    _LIT(KStartButtonHelpText,"");    
    iStartButton = CAknButton::NewL( NULL, NULL, NULL, NULL,
    		KStartButtonText, KStartButtonHelpText, 0, 0 );
    iStartButton->SetContainerWindowL(*this);
    iStartButton->SetRect(TRect ( TPoint(20,180), TSize(200,100) ) );
    iStartButton->SetObserver(this);
    iStartButton->MakeVisible(ETrue);
    iStartButton->ActivateL();

	//create button from resource file
    //set the button rect at SizeChanged()
    iRankButton = CAknButton::NewL();
	iRankButton->ConstructFromResourceL(R_RANK_BUTTON);
	iRankButton->SetContainerWindowL(*this);
	iRankButton->SetObserver(this);
	iRankButton->MakeVisible(ETrue);
	iRankButton->ActivateL();
	}
// End of File  
